<?php

class NewslettersAction extends CommonAction {

    public function index() {
//            die(".............");
        $M = M("Newsletters");
//        die(".............");
        $count = $M->count();
        import("ORG.Util.Page");       //载入分页类
        $page = new Page($count, 20);
        $showPage = $page->show();
        $this->assign("page", $showPage);
        $this->assign("list", D("Newsletters")->listNewsletters($page->firstRow, $page->listRows));
        $this->display();
    }


    public function add() {
        if (IS_POST) {
            $this->checkToken();
            echo json_encode(D("Newsletters")->addNewsletters());
        } else {
            $this->display();
        }
    }

    public function addNew(){
        set_time_limit(0);
        import('ORG.Spider.simple_html_dom');
        $info = array();
        $p_id = $_GET['id'];

        $info['www_url'] = $_GET['source'];

        //信息抓取
        $html = file_get_html('http://www.36kr.com/clipped/'.$p_id);

        $time_dom = $html->find('div.body small',0);
        $info['published'] = explode('/',$time_dom->plaintext);
        $info['published'] = str_replace(array('年','月','日'),array('-','-',''),$info['published'][1].':'.rand(1,60));
        $info['published'] = strtotime($info['published']);

        $info['title'] = $html->find('div.body h4 a',0)->plaintext;

        foreach($html->find('div.body div.krContent ol li') as $k=>$v){
            $info['summary'] .= $v->plaintext.' ';
        }
        $info['source_content'] = str_replace(array('<div>','</div>'),'',$html->find('div.body div.accordion-inner div div',0)->outertext);
        $info['source_content'] = rep_img_src($info['source_content']);

        $info['source_name'] = $html->find('div.body div.accordion-heading a',0)->plaintext;
        $info['source_name'] = explode(',',$info['source_name']);
        $info['source_name'] = str_replace('）','',$info['source_name'][1]);

        $info['source_url'] = str_replace('www.36kr.com','www.whoopsfeed.com',$html->find('div.body div.accordion-inner em a',0)->href);

        $info['aid'] = $_SESSION['my_info']['aid'];

        //去除 36氪站 新闻
        if(strpos($info['title'],'36kr') || strpos($info['title'],'氪') || strpos($info['source_content'],'36kr') || strpos($info['source_content'],'氪')){
            $info['status'] = 0;
            //echo json_encode(array("status" => 1, "id" => "is 36kr new~"));
            //exit;
        }else{
            $info['status'] = 1;
        }

        $news = M('Newsletters');

        $result = $news->add($info);

        echo json_encode(array("status" => 1, "id" => $result));

        $html->clear();

        exit;
    }

    public function spiderBatch(){
        set_time_limit(0);
        $page = @$_GET['page'];
        $type = @$_GET['type'];
        if(isset($type) && $type != null){
            import('ORG.Spider.simple_html_dom');
            $html = file_get_html('http://www.36kr.com/815?page='.$page);

            $arr = array();

            $new_info_dom = $html->find('div.blogPost');

            foreach($new_info_dom as $k=>$v){

                $info = array();

                $info['source'] = explode('/',$v->find('small',0)->plaintext);
                $info['source'] = $info['source'][1];
                $info['href'] = 'http://www.36kr.com'.$v->find('h4 a',0)->href;
                $arr[$k] = $info;

            }

            echo json_encode(array("status" => 1, "info" => array_reverse($arr)));
            $html->clear();

            exit;
        }
        $this->assign('type','Batch');
        $this->display('spider');
    }

    public function spiderSingle(){
        set_time_limit(0);
        $p_id = @$_GET['p_id'];
        if (IS_POST) {
            $this->checkToken();

            echo json_encode(D("Newsletters")->addNewsletters());
            exit;
        }elseif($p_id){
            import('ORG.Spider.simple_html_dom');

            $html = file_get_html('http://www.36kr.com/clipped/'.$p_id);

            $info = array();

            $time_dom = $html->find('div.body small',0);
            $info['time'] = explode('/',$time_dom->plaintext);
            $info['time'] = str_replace(array('年','月','日'),array('-','-',''),$info['time'][1].rand(1,60));
            $info['title'] = $html->find('div.body h4 a',0)->plaintext;

            foreach($html->find('div.body div.krContent ol li') as $k=>$v){
                $info['summer'] .= $v->plaintext.'|';
            }
            $info['source_content'] = str_replace(array('<div>','</div>'),'',$html->find('div.body div.accordion-inner div div',0)->outertext);

            $info['source_name'] = $html->find('div.body div.accordion-heading a',0)->plaintext;
            $info['source_name'] = explode(',',$info['source_name']);
            $info['source_name'] = str_replace('）','',$info['source_name'][1]);

            $info['source_url'] = str_replace('www.36kr.com','www.whoopsfeed.com',$html->find('div.body div.accordion-inner em a',0)->href);

            echo json_encode(array("status" => 1, "info" => $info));
            $html->clear();

            exit;
        }
        $this->assign('type','Single');
        $this->display('spider');
    }

    public function checkNewslettersTitle() {
        $M = M("Newsletters");
        $where = "title='" . $this->_get('title') . "'";
        if (!empty($_GET['id'])) {
            $where.=" And id !=" . (int) $_GET['id'];
        }
        if ($M->where($where)->count() > 0) {
            echo json_encode(array("status" => 0, "info" => "已经存在，请修改标题"));
        } else {
            echo json_encode(array("status" => 1, "info" => "可以使用"));
        }
    }

    public function edit() {
        $M = M("Newsletters");
        if (IS_POST) {
            $this->checkToken();
            echo json_encode(D("Newsletters")->edit());
        } else {
            $info = $M->where("id=" . (int) $_GET['id'])->find();
            if ($info['id'] == '') {
                $this->error("不存在该记录");
            }
            $this->assign("info", $info);
            $this->display("add");
        }
    }

    public function del() {
        if (M("Newsletters")->where("id=" . (int) $_GET['id'])->delete()) {
            $this->success("成功删除");
//            echo json_encode(array("status"=>1,"info"=>""));
        } else {
            $this->error("删除失败，可能是不存在该ID的记录");
        }
    }

}